var express = require('express');
var router = express.Router();
var sqlite3 = require('sqlite3').verbose();
var path = require('path');
/* GET users listing. */
router.get('/', function(req, res, next) {
    var page = req.query.page;
    var limit = req.query.limit;
    var db=new sqlite3.Database(path.join(__dirname, "../db/LH.s3db"));
    try{
        sqlStr = "SELECT \
            manager.[m_name],\
            a_name,\
            s_time, \
            e_time,\
            type,\
            s_num,\
            status\
          FROM \
            (activity JOIN \
            manager on activity.[m_id] = manager.[m_id])";
        if(req.query.tab=="1")
        {
            sqlStr=sqlStr+"where status='1'";
        }
        else if(req.query.tab=="2")
        {
            sqlStr=sqlStr+"where status='2'";
        }
        else if(req.query.tab=="3")
        {
            sqlStr=sqlStr+"where status='-1'";
        }
        else if(req.query.tab=="4")
        {
            sqlStr=sqlStr+"where status='0'";
        }
            var manager = req.session.username;
            sqlStrfinal = sqlStr+" and activity.[m_id]= ? "+"limit ?,?";
            var para = [manager,(page-1)*limit,limit];
            db.all(sqlStrfinal, para,function(err,rows){
                if(!rows){
                    table = {"code":0,"msg":"","count":0,"data":""};
                }else{
                    console.log(rows);
                    table = {"code":0,"msg":"","count":rows.length,"data":""};
                    for(var i=0;i<rows.length;i++){
                        if(rows[i].status=='1')
                            rows[i].status='已通过';
                        else if(rows[i].status=='2')
                            rows[i].status='待修改';
                        else if(rows[i].status=='-1')
                            rows[i].status='未通过';
                        else if(rows[i].status=='0')
                            rows[i].status='待审核';
                        else
                            rows[i].status='出错啦';
                    }
                    table.data=rows;
                }
                res.status(200).json(table);
            } );
    }catch(ex){
        console.log(ex);
    }
});

module.exports = router;
